import { _decorator, Label, Sprite, SpriteAtlas, Toggle } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { IDwGameEnd } from '../proto/gameZt';
import { ResManager } from '../../../../Framework/Scripts/Managers/ResManager';
import { AbNames, EventMsgKes } from '../../Config/ResConfig';
import { RewardType } from '../../Config/GameCfg';
const { ccclass, property } = _decorator;

//主题赛 段位奖励领取
@ccclass('GameDwEndZtGet_Ctr')
export class GameDwEndZtGet_Ctr extends UIComponent {
    spIcon: Sprite;
    labNum: Label;
    tog: Toggle;
    _click: boolean;
    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.Init();

        this.AddButtonListener('root/btn',this,this.OnBtnGetClick);
      
    }

    private Init(){
        this.spIcon = this.ViewComponent('root/logo/icon',Sprite);
        this.labNum = this.ViewComponent('root/labNum',Label);
        this.tog = this.ViewComponent('root/Node/Toggle',Toggle);
    }

    private InitData(){
   
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgThemeMatchGetDwReward,this._onGetDwRewardRes,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgThemeMatchGetDwReward,this._onGetDwRewardRes,this);
    }

    private OnBtnGetClick(){
        if(this._click === false){
            return;
        }
        this._click = false;

        let bei = this.tog.isChecked ? 2:1;
        globalThis.gameMgr._GameZtMsg.sendGetDwReward(bei);
    }

    public setData(data:IDwGameEnd){
        console.log('===========GameDwEndZtGet_Ctr===setData===========',data)
        if(!this.spIcon){
            this.Init();
        }

        if(this.hasProInObj(data,'rewardNum')){
            this.labNum.string = data.rewardNum.toString();
        }

        let atals = ResManager.inst.getAsset(AbNames.GamePlay,'Atals/GamePlay',SpriteAtlas) as SpriteAtlas;
        if(this.hasProInObj(data,'rewardType')){
            let rdType = data.rewardType;
            let sf = null;
            if(rdType === RewardType.TYpe_QiuNum){
                sf =  atals.getSpriteFrame('GameReward_coin');
            }

            if(sf){
                this.spIcon.spriteFrame = sf;
            }
        }
    }

    //领取段位结算奖励
    private _onGetDwRewardRes(){
        this.node.destroy();
    }
}


